package com.yuyang.gulimall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuyang.common.to.MemberPrice;
import com.yuyang.common.to.SkuReductionTo;
import com.yuyang.common.utils.PageUtils;
import com.yuyang.common.utils.Query;
import com.yuyang.gulimall.coupon.dao.SkuFullReductionDao;
import com.yuyang.gulimall.coupon.entity.MemberPriceEntity;
import com.yuyang.gulimall.coupon.entity.SkuFullReductionEntity;
import com.yuyang.gulimall.coupon.entity.SkuLadderEntity;
import com.yuyang.gulimall.coupon.service.MemberPriceService;
import com.yuyang.gulimall.coupon.service.SkuFullReductionService;
import com.yuyang.gulimall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderService skuLadderService;
    @Autowired
    MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(SkuReductionTo skuReductionTo) {
// sku的满减、折扣、会员价格等  sms_sku_ladder、sms_member_price、sms_sku_full_reduction
        Long skuId = skuReductionTo.getSkuId();
        if (skuReductionTo.getFullCount() > 0) {
            SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
            BeanUtils.copyProperties(skuReductionTo, skuLadderEntity);
            skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
            skuLadderService.save(skuLadderEntity);
        }

        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        List<MemberPriceEntity> memberPriceEntities = memberPrice.stream().
                filter(item -> item.getPrice().compareTo(new BigDecimal("0")) == 1).
                map(item -> {
                    MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                    memberPriceEntity.setSkuId(skuId);
                    memberPriceEntity.setMemberPrice(item.getPrice());
                    memberPriceEntity.setMemberLevelName(item.getName());
                    memberPriceEntity.setAddOther(1);
                    memberPriceEntity.setMemberLevelId(item.getId());
                    return memberPriceEntity;
                }).collect(Collectors.toList());
        memberPriceService.saveBatch(memberPriceEntities);

        if (skuReductionTo.getFullPrice().compareTo(new BigDecimal("0")) == 1) {
            SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
            BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
            skuFullReductionEntity.setAddOther(skuReductionTo.getPriceStatus());
            this.save(skuFullReductionEntity);
        }
    }

}